﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.CodeDom;

namespace Perovich.GameObjects.Tools.Util
{
    public class AutoProperty
    {
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        private string summary;

        public string Summary
        {
            get { return summary; }
            set { summary = value; }
        }

        private string value;

        public string Value
        {
            get { return value; }
            set { this.value = value; }
        }

        public bool IsReadOnly { get; set; }

        public CodeMemberProperty Prop
        {
            get
            {
                var p = new CodeMemberProperty();
                p.Attributes = attri;
                p.Name = name;
                p.HasGet = true;
                p.HasSet = !IsReadOnly;
                p.Type = new CodeTypeReference(type);
                CodeExpression field;
                if (HasField)
                {
                    field = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), string.Concat("_", name));
                }
                else
                {
                    field = target;
                }
                p.GetStatements.Add(new CodeMethodReturnStatement(field));
                if (!IsReadOnly) p.SetStatements.Add(new CodeAssignStatement(field, new CodePropertySetValueReferenceExpression()));
                if (!string.IsNullOrEmpty(Summary)) p.Comments.Add(new CodeCommentStatement("<summary>" + Summary + "</summary>", true));
                if (!string.IsNullOrEmpty(Value)) p.Comments.Add(new CodeCommentStatement("<returns>" + Value + "</returns>", true));
                return p;
            }
        }

        public CodeMemberField Fie
        {
            get
            {
                var f = new CodeMemberField();
                f.Attributes = MemberAttributes.Private;
                f.Name = string.Concat("_", name);
                f.Type = new CodeTypeReference(type);
                return f;
            }
        }

        public bool HasField
        {
            get
            {
                return (target == null);
            }
        }
        private MemberAttributes attri;
        private CodeExpression target;
        private string type;

        public string Type
        {
            get { return type; }
            set { type = value; }
        }
        public AutoProperty(string name, string type, CodeExpression target = null, MemberAttributes attri = MemberAttributes.Public | MemberAttributes.Final)
        {
            this.name = name;
            this.type = type;
            this.target = target;
            this.attri = attri;
            IsReadOnly = false;
        }

    }
}
